<!DOCTYPE html>
<html lang="en" data-color-mode="auto" data-light-theme="light" data-dark-theme="dark">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>G4F - Docker Setup</title>
    <link rel="apple-touch-icon" sizes="180x180" href="/dist/img/apple-touch-icon.png">
    <link rel="icon" type="image/png" sizes="32x32" href="/dist/img/favicon-32x32.png">
    <link rel="icon" type="image/png" sizes="16x16" href="/dist/img/favicon-16x16.png">
    <link rel="manifest" href="/dist/img/site.webmanifest">
    <link crossorigin="anonymous" media="all" rel="stylesheet" href="https://github.githubassets.com/assets/light-74231a1f3bbb.css" />
    <link crossorigin="anonymous" media="all" rel="stylesheet" href="https://github.githubassets.com/assets/dark-8a995f0bacd4.css" />
    <link crossorigin="anonymous" media="all" rel="stylesheet" href="https://github.githubassets.com/assets/primer-primitives-225433424a87.css" />
    <link crossorigin="anonymous" media="all" rel="stylesheet" href="https://github.githubassets.com/assets/primer-b8b91660c29d.css" />
    <link crossorigin="anonymous" media="all" rel="stylesheet" href="https://github.githubassets.com/assets/global-205098e9fedd.css" />
    <link crossorigin="anonymous" media="all" rel="stylesheet" href="https://github.githubassets.com/assets/code-177d21388df8.css" />
    <style>
        :root {
            --colour-1: #000000;
            --colour-2: #ccc;
            --colour-3: #e4d4ff;
            --colour-4: #f0f0f0;
            --colour-5: #181818;
            --colour-6: #242424;
            --accent: #8b3dff;
            --gradient: #1a1a1a;
            --background: #16101b;
            --size: 70vw;
            --top: 50%;
            --blur: 40px;
            --opacity: 0.6;
        }

        /* Body and text color */
        body {
            height: 100vh;
            margin: 0;
            padding: 0;
        }

        .hidden {
            display: none;
        }

        .container-lg {
            margin: 0 auto;
            padding: 8px;
        }

        @media only screen and (min-width: 40em) {
            .container-lg {
                max-width: 84%;
            }
        }
    </style>
</head>
<body>
    <article class="markdown-body entry-content container-lg" itemprop="text"><div class="markdown-heading"><h2 class="heading-element">G4F - Docker Setup</h2><a id="user-content-g4f---docker-setup" class="anchor" aria-label="Permalink: G4F - Docker Setup" href="#g4f---docker-setup"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<div class="markdown-heading"><h2 class="heading-element">Table of Contents</h2><a id="user-content-table-of-contents" class="anchor" aria-label="Permalink: Table of Contents" href="#table-of-contents"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<ul>
<li><a href="#prerequisites">Prerequisites</a></li>
<li><a href="#installation-and-setup">Installation and Setup</a></li>
<li><a href="#testing-the-api">Testing the API</a></li>
<li><a href="#troubleshooting">Troubleshooting</a></li>
<li><a href="#stopping-the-service">Stopping the Service</a></li>
</ul>
<div class="markdown-heading"><h2 class="heading-element">Prerequisites</h2><a id="user-content-prerequisites" class="anchor" aria-label="Permalink: Prerequisites" href="#prerequisites"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<p><strong>Before you begin, ensure you have the following installed on your system:</strong></p>
<ul>
<li><a href="https://docs.docker.com/get-docker/" rel="nofollow">Docker</a></li>
<li><a href="https://docs.docker.com/compose/install/" rel="nofollow">Docker Compose</a></li>
<li>Python 3.7 or higher</li>
<li>pip (Python package manager)</li>
</ul>
<p><strong>Note:</strong> If you encounter issues with Docker, you can run the project directly using Python.</p>
<div class="markdown-heading"><h2 class="heading-element">Installation and Setup</h2><a id="user-content-installation-and-setup" class="anchor" aria-label="Permalink: Installation and Setup" href="#installation-and-setup"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<div class="markdown-heading"><h3 class="heading-element">Docker Method (Recommended)</h3><a id="user-content-docker-method-recommended" class="anchor" aria-label="Permalink: Docker Method (Recommended)" href="#docker-method-recommended"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<ol>
<li>
<p><strong>Clone the Repository</strong></p>
<div class="highlight highlight-source-shell"><pre>git clone https://github.com/xtekky/gpt4free.git
<span class="pl-c1">cd</span> gpt4free</pre></div>
</li>
<li>
<p><strong>Build and Run with Docker Compose</strong></p>
<p>Pull the latest image and run a container with Google Chrome support:</p>
<div class="highlight highlight-source-shell"><pre>   docker pull hlohaus789/g4f
   docker-compose up -d</pre></div>
<p>Or run the small docker images without Google Chrome:</p>
<div class="highlight highlight-source-shell"><pre>   docker-compose -f docker-compose-slim.yml up -d</pre></div>
</li>
<li>
<p><strong>Access the API or the GUI</strong></p>
<p>The api server will be accessible at <code>http://localhost:1337</code></p>
<p>And the gui at this url: <code>http://localhost:8080</code></p>
</li>
</ol>
<div class="markdown-heading"><h3 class="heading-element">Non-Docker Method</h3><a id="user-content-non-docker-method" class="anchor" aria-label="Permalink: Non-Docker Method" href="#non-docker-method"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<p>If you encounter issues with Docker, you can run the project directly using Python:</p>
<ol>
<li>
<p><strong>Clone the Repository</strong></p>
<div class="highlight highlight-source-shell"><pre>git clone https://github.com/xtekky/gpt4free.git
<span class="pl-c1">cd</span> gpt4free</pre></div>
</li>
<li>
<p><strong>Install Dependencies</strong></p>
<div class="highlight highlight-source-shell"><pre>pip install -r requirements.txt</pre></div>
</li>
<li>
<p><strong>Run the Server</strong></p>
<div class="highlight highlight-source-shell"><pre>python -m g4f.api.run</pre></div>
</li>
<li>
<p><strong>Access the API or the GUI</strong></p>
<p>The api server will be accessible at <code>http://localhost:1337</code></p>
<p>And the gui at this url: <code>http://localhost:8080</code></p>
</li>
</ol>
<div class="markdown-heading"><h2 class="heading-element">Testing the API</h2><a id="user-content-testing-the-api" class="anchor" aria-label="Permalink: Testing the API" href="#testing-the-api"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<p><strong>You can test the API using curl or by creating a simple Python script:</strong></p>
<div class="markdown-heading"><h3 class="heading-element">Using curl</h3><a id="user-content-using-curl" class="anchor" aria-label="Permalink: Using curl" href="#using-curl"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<div class="highlight highlight-source-shell"><pre>curl -X POST -H <span class="pl-s"><span class="pl-pds">"</span>Content-Type: application/json<span class="pl-pds">"</span></span> -d <span class="pl-s"><span class="pl-pds">'</span>{"prompt": "What is the capital of France?"}<span class="pl-pds">'</span></span> http://localhost:1337/chat/completions</pre></div>
<div class="markdown-heading"><h3 class="heading-element">Using Python</h3><a id="user-content-using-python" class="anchor" aria-label="Permalink: Using Python" href="#using-python"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<p><strong>Create a file named <code>test_g4f.py</code> with the following content:</strong></p>
<div class="highlight highlight-source-python"><pre><span class="pl-k">import</span> <span class="pl-s1">requests</span>

<span class="pl-s1">url</span> <span class="pl-c1">=</span> <span class="pl-s">"http://localhost:1337/v1/chat/completions"</span>
<span class="pl-s1">body</span> <span class="pl-c1">=</span> {
    <span class="pl-s">"model"</span>: <span class="pl-s">"gpt-4o-mini"</span>, 
    <span class="pl-s">"stream"</span>: <span class="pl-c1">False</span>,
    <span class="pl-s">"messages"</span>: [
        {<span class="pl-s">"role"</span>: <span class="pl-s">"assistant"</span>, <span class="pl-s">"content"</span>: <span class="pl-s">"What can you do?"</span>}
    ]
}

<span class="pl-s1">json_response</span> <span class="pl-c1">=</span> <span class="pl-s1">requests</span>.<span class="pl-c1">post</span>(<span class="pl-s1">url</span>, <span class="pl-s1">json</span><span class="pl-c1">=</span><span class="pl-s1">body</span>).<span class="pl-c1">json</span>().<span class="pl-c1">get</span>(<span class="pl-s">'choices'</span>, [])

<span class="pl-k">for</span> <span class="pl-s1">choice</span> <span class="pl-c1">in</span> <span class="pl-s1">json_response</span>:
    <span class="pl-en">print</span>(<span class="pl-s1">choice</span>.<span class="pl-c1">get</span>(<span class="pl-s">'message'</span>, {}).<span class="pl-c1">get</span>(<span class="pl-s">'content'</span>, <span class="pl-s">''</span>))</pre></div>
<p><strong>Run the script:</strong></p>
<div class="highlight highlight-source-shell"><pre>python test_g4f.py</pre></div>
<div class="markdown-heading"><h2 class="heading-element">Troubleshooting</h2><a id="user-content-troubleshooting" class="anchor" aria-label="Permalink: Troubleshooting" href="#troubleshooting"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<ul>
<li>If you encounter issues with Docker, try running the project directly using Python as described in the Non-Docker Method.</li>
<li>Ensure that you have the necessary permissions to run Docker commands. You might need to use <code>sudo</code> or add your user to the <code>docker</code> group.</li>
<li>If the server doesn't start, check the logs for any error messages and ensure all dependencies are correctly installed.</li>
</ul>
<p><strong><em>For more detailed information on API endpoints and usage, refer to the <a href="/docs/interference-api.html">G4F API documentation</a>.</em></strong></p>
<div class="markdown-heading"><h2 class="heading-element">Stopping the Service</h2><a id="user-content-stopping-the-service" class="anchor" aria-label="Permalink: Stopping the Service" href="#stopping-the-service"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<div class="markdown-heading"><h3 class="heading-element">Docker Method</h3><a id="user-content-docker-method" class="anchor" aria-label="Permalink: Docker Method" href="#docker-method"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<p><strong>To stop the Docker containers, use the following command:</strong></p>
<div class="highlight highlight-source-shell"><pre>docker-compose down</pre></div>
<div class="markdown-heading"><h3 class="heading-element">Non-Docker Method</h3><a id="user-content-non-docker-method-1" class="anchor" aria-label="Permalink: Non-Docker Method" href="#non-docker-method-1"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<p>If you're running the server directly with Python, you can stop it by pressing Ctrl+C in the terminal where it's running.</p>
<hr>
<p><a href="/docs/">Return to Documentation</a></p>
</article>
</body>
</html>